package com.qianli.user.domain.respository;

import com.fqgj.xjd.user.dao.UserFrozenCodeMapDao;
import com.fqgj.xjd.user.entity.UserFrozenCodeMapEntity;
import com.google.common.collect.Lists;
import com.qianli.user.domain.model.UserFrozenModel;
import com.qianli.user.domain.model.frozen.UserFrozen;
import com.qianli.user.enums.FrozenCodeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * name:用户冷冻
 * auth:ting.li@qianli-inc.com
 * date:2018/1/3 20:03
 * version: v1.0.0
 **/
@Repository
public class UserFrozenRespository implements BaseRespository<UserFrozenModel>{

    @Autowired
    private UserFrozenCodeMapDao userFrozenCodeMapDao;

    @Override
    public void load(UserFrozenModel model) {
        List<UserFrozenCodeMapEntity> entities = userFrozenCodeMapDao.selectUserFrozenCodeMapByUserCode(model.getUserCode());
        if(CollectionUtils.isEmpty(entities)){
            return;
        }
        List<UserFrozen> userFrozens = Lists.newArrayList();
        for(UserFrozenCodeMapEntity entity:entities){
            UserFrozen userFrozen = new UserFrozen();
            BeanUtils.copyProperties(entity,userFrozen);
            userFrozen.setDesc(FrozenCodeEnum.getEnumByCode(userFrozen.getCode()).getDesc());
            userFrozens.add(userFrozen);
        }
        model.setFrozens(userFrozens);
    }

    @Override
    public void store(UserFrozenModel model) {

        UserFrozen userFrozen = model.getFrozens().get(0);
        UserFrozenCodeMapEntity codeMapEntity = new UserFrozenCodeMapEntity();
        BeanUtils.copyProperties(userFrozen,codeMapEntity);
        userFrozenCodeMapDao.insert(codeMapEntity);
    }

    @Override
    public void restore(UserFrozenModel model) {

    }
}
